home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / TupleDatabase / DiskEntry.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  12.3 KB  |  596 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DiskEntry.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef    __DISKENTRY__
  15. #include "DiskEntry.h"
  16. #endif
  17.  
  18. #ifndef __ABSTRACTFILE__
  19. #include "AbstractFile.h"
  20. #endif
  21.  
  22. #ifndef    __STDIO__
  23. #include "StdIO.h"
  24. #endif
  25.  
  26. #ifndef    __STRING__
  27. #include "String.h"
  28. #endif
  29.  
  30. #ifndef    __IOSTREAM__
  31. #include "IOStream.h"
  32. #endif
  33.  
  34. #ifndef    __STDLIB__
  35. #include "StdLib.h"
  36. #endif
  37.  
  38. #pragma segment DiskLog
  39.  
  40. /***********************************|****************************************/
  41.  
  42. const EntryID TLogEntry::kInvalidID = (EntryID) 0;    // should be zero
  43. const unsigned long TTestEntry::kChunkSize = 64;
  44. const char* const kSampleData = "all work and no play makes keith a dull boy - ";
  45.  
  46. /***********************************|****************************************/
  47. /***********************************|****************************************/
  48.  
  49. TLogEntry::TLogEntry ():
  50.     fID ( kInvalidID ),
  51.     fTime (),
  52.     fCache ( nil )
  53. {
  54.     fTime.SetToLocalTime ();
  55. }
  56.  
  57. /***********************************|****************************************/
  58.  
  59. TLogEntry::TLogEntry ( const TLogEntry& that ):
  60.     fID ( that.fID ),
  61.     fTime ( that.fTime ),
  62.     fCache ( nil )
  63. {
  64. }
  65.  
  66. /***********************************|****************************************/
  67.  
  68. TLogEntry&
  69. TLogEntry::operator = ( const TLogEntry& that )
  70. {
  71.     if ( this != &that )
  72.     {
  73.         fID = that.fID;
  74.         fTime = that.fTime;
  75.         delete fCache;
  76.         fCache = nil;
  77.     }
  78.  
  79.     return *this;
  80. }
  81.  
  82. /***********************************|****************************************/
  83.  
  84. Boolean
  85. TLogEntry::operator == ( const TLogEntry& that ) const
  86. {
  87.     return ( fID == that.fID ) && ( fTime == that.fTime );
  88. }
  89.  
  90. /***********************************|****************************************/
  91.  
  92. Boolean
  93. TLogEntry::operator != ( const TLogEntry& that ) const
  94. {
  95.     return !operator == ( that );
  96. }
  97.  
  98. /***********************************|****************************************/
  99.  
  100. unsigned long
  101. TLogEntry::GetTextLength () const
  102. {
  103.     if ( !fCache )
  104.         ( (TLogEntry*) this )->fCache = CreateText ();
  105.  
  106.     return strlen ( fCache );
  107. }
  108.  
  109. /***********************************|****************************************/
  110.  
  111. void
  112. TLogEntry::GetText ( char* buffer, unsigned long size ) const
  113. {
  114.     if ( !fCache )
  115.         ( (TLogEntry*) this )->fCache = CreateText ();
  116.  
  117.     strncpy ( buffer, fCache, (int) size );
  118. }
  119.  
  120. /***********************************|****************************************/
  121.  
  122. char*
  123. TLogEntry::CreateText () const
  124. {    char buffer [ 256 ];
  125.  
  126.     const char* kFormat = "LogEntry %li (%s)\n";
  127.     char* timeString = fTime.CreateString ();
  128.     unsigned long length = sprintf ( buffer, kFormat, fID, timeString );
  129.     char* finalString = new char [ length + 1 ];
  130.     if ( finalString )
  131.     {
  132.         memcpy ( finalString, buffer, length + 1 );
  133.     }
  134.     delete timeString;
  135.     
  136.     return finalString;
  137. }
  138.  
  139. /***********************************|****************************************/
  140.  
  141. Boolean
  142. TLogEntry::WriteTo ( TAbstractFile& file ) const
  143. {
  144.     if ( file.WriteDataIgnore ( &( (TLogEntry*) this )->fID, sizeof ( fID ) ) != noErr )
  145.         return false;
  146.     return fTime.WriteTo ( file );
  147. }
  148.  
  149. /***********************************|****************************************/
  150.  
  151. Boolean
  152. TLogEntry::ReadFrom ( TAbstractFile& file )
  153. {
  154.     if ( file.ReadDataIgnore ( &fID, sizeof ( fID ) ) != noErr )
  155.         return false;
  156.     return fTime.ReadFrom ( file );
  157. }
  158.  
  159. /***********************************|****************************************/
  160.  
  161. const char*
  162. TLogEntry::GetClass () const
  163. {
  164.     return "TLogEntry";
  165. }
  166.  
  167. /***********************************|****************************************/
  168.  
  169. ostream&
  170. TLogEntry::operator >> ( ostream& stream ) const
  171. {
  172.     stream << GetClass () << ":" << (void*) this << "\n";
  173.     stream << "\tfID:" << fID << "\n\tfTime:";
  174.     fTime >> stream;
  175.     stream.flush ();
  176.     return stream;
  177. }
  178.  
  179. /***********************************|****************************************/
  180. /***********************************|****************************************/
  181.  
  182. #pragma trace off
  183.  
  184. TTestEntry::TTestEntry ():
  185.     TLogEntry (),
  186.     fLength ( RandomLength () )
  187. {
  188. }
  189.  
  190. /***********************************|****************************************/
  191.  
  192. TTestEntry::TTestEntry ( unsigned long length ):
  193.     TLogEntry (),
  194.     fLength ( length )
  195. {
  196. }
  197.  
  198. /***********************************|****************************************/
  199.  
  200. TTestEntry::~TTestEntry ()
  201. {
  202. }
  203.  
  204. /***********************************|****************************************/
  205.  
  206. TTestEntry::TTestEntry ( const TTestEntry& that ):
  207.     TLogEntry ( that ),
  208.     fLength ( that.fLength )
  209. {
  210. }
  211.  
  212. /***********************************|****************************************/
  213.  
  214. TTestEntry&
  215. TTestEntry::operator = ( const TTestEntry& that )
  216. {
  217.     if ( this != &that )
  218.     {
  219.         TLogEntry::operator = ( that );
  220.         fLength = that.fLength;
  221.     }
  222.  
  223.     return *this;
  224. }
  225.  
  226. /***********************************|****************************************/
  227.  
  228. const char*
  229. TTestEntry::GetClass () const
  230. {
  231.     return "TTestEntry";
  232. }
  233.  
  234. /***********************************|****************************************/
  235.  
  236. Boolean
  237. TTestEntry::WriteTo ( TAbstractFile& file ) const
  238. {
  239.     if ( !TLogEntry::WriteTo ( file ) )
  240.         return false;
  241.  
  242.     if ( file.WriteDataIgnore ( &( (TTestEntry*) this )->fLength, sizeof ( fLength ) ) != noErr )
  243.         return false;
  244.  
  245.     unsigned long remaining = fLength, nextWrite;
  246.     char buffer [ kChunkSize ];
  247.     FillBuffer ( buffer, sizeof ( buffer ) );
  248.  
  249.     while ( remaining > 0 )
  250.     {
  251.         nextWrite = remaining > kChunkSize ? kChunkSize : remaining;
  252.         remaining -= nextWrite;
  253.  
  254.         if ( file.WriteDataIgnore ( buffer, nextWrite ) != noErr )
  255.             return false;
  256.     }
  257.  
  258.     return true;
  259. }
  260.  
  261. /***********************************|****************************************/
  262.  
  263. Boolean
  264. TTestEntry::ReadFrom ( TAbstractFile& file )
  265. {
  266.     if ( !TLogEntry::ReadFrom ( file ) )
  267.         return false;
  268.  
  269.     if ( file.ReadDataIgnore ( &fLength, sizeof ( fLength ) ) != noErr )
  270.         return false;
  271.  
  272.     unsigned long remaining = fLength, nextRead;
  273.     char buffer [ kChunkSize ];
  274.  
  275.     while ( remaining > 0 )
  276.     {
  277.         nextRead = remaining > kChunkSize ? kChunkSize : remaining;
  278.         remaining -= nextRead;
  279.  
  280.         if ( file.ReadDataIgnore ( buffer, nextRead ) != noErr )
  281.             return false;
  282.  
  283.         if ( !VerifyBuffer ( buffer, nextRead ) )
  284.             return false;
  285.     }
  286.  
  287.     return true;
  288. }
  289.  
  290. /***********************************|****************************************/
  291.  
  292. inline char
  293. BufferChar ( unsigned long index )
  294. {
  295.     return '='; // 'A' + ( ( 'z' - 'A' ) % (char) ( index + 1 ) ) - 1;
  296. }
  297.  
  298. /***********************************|****************************************/
  299.  
  300. void
  301. TTestEntry::FillBuffer ( char* buffer, unsigned long length )
  302. {
  303.     for ( unsigned long i = 0; i < length; i++ )
  304.         *buffer++ = BufferChar ( i );
  305. }
  306.  
  307. /***********************************|****************************************/
  308.  
  309. Boolean
  310. TTestEntry::VerifyBuffer ( char* buffer, unsigned long length )
  311. {
  312.     for ( unsigned long i = 0; i < length; i++ )
  313.         if ( *buffer++ != BufferChar ( i ) )
  314.             return false;
  315.  
  316.     return true;
  317. }
  318.  
  319. /***********************************|****************************************/
  320.  
  321. ostream&
  322. TTestEntry::operator >> ( ostream& stream ) const
  323. {
  324.     TLogEntry::operator >> ( stream );
  325.     stream << "\tfLength:" << fLength << "\n";
  326.     return stream;
  327. }
  328.  
  329. /***********************************|****************************************/
  330.  
  331. unsigned long
  332. TTestEntry::RandomLength ( unsigned long min, unsigned long max )
  333. {
  334.     return  min + ( rand() % ( max - min ) );
  335. }
  336.  
  337. /***********************************|****************************************/
  338.  
  339. Boolean
  340. TTestEntry::operator == ( const TTestEntry& that ) const
  341. {
  342.     return
  343.         TLogEntry::operator == ( (TLogEntry&) (TTestEntry&) that ) &&
  344.         ( fLength == that.fLength );
  345. }
  346.  
  347. /***********************************|****************************************/
  348.  
  349. Boolean
  350. TTestEntry::operator != ( const TTestEntry& that ) const
  351. {
  352.     return !operator == ( that );
  353. }
  354.  
  355. /***********************************|****************************************/
  356. /***********************************|****************************************/
  357.  
  358. TErrorEntry::TErrorEntry ():
  359.     TLogEntry (),
  360.     fFile ( nil ),
  361.     fLine ( 0 ),
  362.     fMessage ( nil )
  363. {
  364. }
  365.  
  366. /***********************************|****************************************/
  367.  
  368. TErrorEntry::TErrorEntry ( const TErrorEntry& that ):
  369.     TLogEntry ( that ),
  370.     fFile ( nil ),
  371.     fLine ( 0 ),
  372.     fMessage ( nil )
  373. {
  374.     if ( that.fFile )
  375.     {
  376.         fFile = new char [ strlen ( that.fFile ) + 1 ];
  377.         strcpy ( fFile, that.fFile );
  378.     }
  379.  
  380.     fLine = that.fLine;
  381.  
  382.     if ( that.fMessage )
  383.     {
  384.         fMessage = new char [ strlen ( that.fMessage ) + 1 ];
  385.         strcpy ( fMessage, that.fMessage );
  386.     }
  387. }
  388.  
  389. /***********************************|****************************************/
  390.  
  391. TErrorEntry&
  392. TErrorEntry::operator = ( const TErrorEntry& that )
  393. {
  394.     if ( this != &that )
  395.     {
  396.         TLogEntry::operator = ( that );
  397.  
  398.         delete fFile;
  399.         if ( that.fFile )
  400.         {
  401.             fFile = new char [ strlen ( that.fFile ) + 1 ];
  402.             strcpy ( fFile, that.fFile );
  403.         }
  404.         else
  405.         {
  406.             fFile = nil;
  407.         }
  408.  
  409.         fLine = that.fLine;
  410.  
  411.         delete fMessage;
  412.         if ( that.fMessage )
  413.         {
  414.             fMessage = new char [ strlen ( that.fMessage ) + 1 ];
  415.             strcpy ( fMessage, that.fMessage );
  416.         }
  417.         else
  418.         {
  419.             fMessage = nil;
  420.         }
  421.     }
  422.  
  423.     return *this;
  424. }
  425.  
  426. /***********************************|****************************************/
  427.  
  428. TErrorEntry::~TErrorEntry ()
  429. {
  430.     delete fFile;
  431.     delete fMessage;
  432. }
  433.  
  434. /***********************************|****************************************/
  435.  
  436. unsigned long
  437. TErrorEntry::GetString ( char* buffer ) const
  438. {
  439.     if ( fFile && fMessage )
  440.     {
  441.         return sprintf ( buffer, "FILE \"%s\"; LINE %li; # %s\n", fFile, fLine, fMessage );
  442.     }
  443.     else if ( fFile )
  444.     {
  445.         return sprintf ( buffer, "FILE \"%s\"; LINE %li\n", fFile, fLine );
  446.     }
  447.     else if ( fMessage )
  448.     {
  449.         return sprintf ( buffer, "# %s\n", fMessage );
  450.     }
  451.     else
  452.     {
  453.         return sprintf ( buffer, "# Unknown error.\n" );
  454.     }
  455. }
  456.  
  457. /***********************************|****************************************/
  458.  
  459. unsigned long
  460. TErrorEntry::GetTextLength () const
  461. {
  462.     return GetString ( nil );
  463. }
  464.  
  465. /***********************************|****************************************/
  466.  
  467. void
  468. TErrorEntry::GetText ( char* buffer, unsigned long size ) const
  469. {
  470.     char* string = CreateText ();
  471.     strncpy ( buffer, string, (unsigned int) size );
  472.     delete string;
  473. }
  474.  
  475. /***********************************|****************************************/
  476.  
  477. char*
  478. TErrorEntry::CreateText () const
  479. {
  480.     char* string = new char [ GetString ( nil ) + 1 ];
  481.     GetString ( string );
  482.     return string;
  483. }
  484.  
  485. /***********************************|****************************************/
  486.  
  487. Boolean
  488. TErrorEntry::WriteTo ( TAbstractFile& file ) const
  489. {
  490.     if ( !Write ( fFile, file ) )
  491.         return false;
  492.  
  493.     if ( file.WriteDataIgnore ( &( (TErrorEntry*) this )->fLine, sizeof ( fLine ) ) != noErr )
  494.         return false;
  495.  
  496.     if ( !Write ( fMessage, file ) )
  497.         return false;
  498.  
  499.     return true;
  500. }
  501.  
  502. /***********************************|****************************************/
  503.  
  504. Boolean
  505. TErrorEntry::ReadFrom ( TAbstractFile& file )
  506. {
  507.     delete fFile;
  508.  
  509.     if ( !Read ( fFile, file ) )
  510.         return false;
  511.  
  512.     if ( file.ReadDataIgnore ( &fLine, sizeof ( fLine ) ) != noErr )
  513.         return false;
  514.  
  515.     delete fMessage;
  516.  
  517.     if ( !Read ( fMessage, file ) )
  518.         return false;
  519.  
  520.     return true;
  521. }
  522.  
  523. /***********************************|****************************************/
  524.  
  525. Boolean
  526. TErrorEntry::Write ( const char* string, TAbstractFile& file ) const
  527. {
  528.     unsigned long length = 0;
  529.  
  530.     if ( string )
  531.         length = strlen ( string );
  532.  
  533.     if ( file.Write ( length ) != noErr )
  534.         return false;
  535.  
  536.     if ( string )
  537.         if ( file.WriteDataIgnore ( string, length ) != noErr )
  538.             return false;
  539.  
  540.     return true;
  541. }
  542.  
  543. /***********************************|****************************************/
  544.  
  545. Boolean
  546. TErrorEntry::Read ( char*& string, TAbstractFile& file ) const
  547. {
  548.     string = nil;
  549.     unsigned long length = 0;
  550.  
  551.     if ( file.Read ( length ) != noErr )
  552.         return false;
  553.  
  554.     if ( length > 0 )
  555.     {
  556.         string = new char [ length + 1 ];
  557.  
  558.         if ( string )
  559.         {
  560.             if ( file.ReadDataIgnore ( string, length ) != noErr )
  561.             {
  562.                 delete string;
  563.                 string = nil;
  564.                 return false;
  565.             }
  566.  
  567.             string [ length ] = 0;        // add null terminator
  568.         }
  569.         else
  570.         {
  571.             file.SetPosition ( fsFromMark, length );    // skip over string data
  572.             return false;
  573.         }
  574.     }
  575.  
  576.     return true;
  577. }
  578.  
  579. /***********************************|****************************************/
  580.  
  581. const char*
  582. TErrorEntry::GetClass () const
  583. {
  584.     return "TErrorEntry";
  585. }
  586.  
  587. /***********************************|****************************************/
  588.  
  589. ostream&
  590. TErrorEntry::operator >> ( ostream& stream ) const
  591. {
  592.     return stream;
  593. }
  594.  
  595. /***********************************|****************************************/
  596.